var app = getApp();
var util = require('../../utils/util.js');
var addressList = null;
Page({
  data: {
    
    // 商品详情介绍
    deschtml:"",
  },
  onLoad: function (options) {
    var that = this;
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
    })
    wx.request({
      url: app.globalData.url + "getProductDetailByProductId",
      data: {
        productId: options.productId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data.data;
        var questionProducts = res.data.questionProducts;
        var imgs = [];
          that.setData({
            producData: data.product,
            deschtml: data.detail,
            imgUrls: imgs,
            inventoryQuantity: data.product.inventoryQuantity,
            originalPrice: data.product.originalPrice,
            price: data.product.minPrice,
            point: data.product.point,
            thumbnailImage: data.product.thumbnailImage,
            productId: data.product.productId,
            productTypeId: data.product.productTypeId,
            listProduct: questionProducts,
            userLevel: data.product.userLevel,
          })
        
          if (that.data.seckillEndTime != undefined){
            that.countTime(that.data.seckillEndTime)
          }
      },
    })

  },
  onShow:function(){
    var that = this;
  },
  onReady: function () {
    var that = this;
    this.dialog = this.selectComponent("#dialog");
  },
  showDialog() {
    var that = this;
    var level = wx.getStorageSync("level");
    console.info(wx.getStorageSync("level"));
    console.info("===="+level);
    if (level >= that.data.userLevel ) {
      this.dialog.showDialog();
    } else {
      wx.showToast({
        title: '提升等级后再来购买',
        icon: 'none',
        duration: 2000
      });
    }
    
  },
  //取消事件
  _cancelEvent() {
    console.log('你点击了取消');
    this.dialog.hideDialog();
  },
  //确认事件
  _confirmEvent() {
    console.log('你点击了确定');
    var that = this;
    this.dialog.hideDialog();
    var points = wx.getStorageSync("points");
    var plist = '[{"productId":"' + that.data.productId + '","num":' + 1 + ',"price":' + that.data.price + ',"title":"' + that.data.producData.title + '","thumbnailImage":"' + that.data.thumbnailImage + '","point":' + that.data.point + ',"seckillNum":' + that.data.producData.seckillLimitNum + '}]';
    console.info(points);
    if (points < that.data.point){
      wx.showToast({
        title: '积分不足',
        icon: 'none',
        duration: 2000
      });
    } else if (that.data.producData.inventoryQuantity == 0) {
      wx.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 2000
      });
    } else {
      wx.navigateTo({
        url: '/pages/integralOrder/integralOrder?price=' + that.data.price + '&thumbnailImage=' + that.data.thumbnailImage + '&point=' + that.data.point + '&title=' + that.data.producData.title + '&productIdAndNumList=' + plist,
      })
    }
  },

  navBack: function () {
    var that = this;
      wx.navigateBack({

      })
    
  },
})


